/************************
 * cxc demo
 * All rights reserved.
 * 2013-3-29 ����10:08:50
 ************************/

package com.cxc.myweb.demo.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cxc.myweb.demo.model.bpo.UserBpo;
import com.cxc.myweb.demo.model.dto.User;
import com.cxc.myweb.demo.util.BasicType;

@SuppressWarnings("rawtypes")
public class UserServlet extends HttpServlet {

	Logger logger = LoggerFactory.getLogger(UserServlet.class);

	private static final long serialVersionUID = 1L;
	private WebApplicationContext ctx;
	
	//有数据库连接情况下BPO类
	private UserBpo userBpo;

	public UserServlet() {
		super();
	}

	public void init(ServletConfig config) {
		try {
			super.init(config);
		} catch (ServletException e) {
			e.printStackTrace();
		}
		ctx = WebApplicationContextUtils.getWebApplicationContext(config
				.getServletContext());
		userBpo = (UserBpo) ctx.getBean("userBpo");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {

		logger.info("DemoServlet start get...");
		if (logger.isDebugEnabled()) {
			// debug mode,check input infos
			request.setAttribute("attr1", "tom");
			java.util.Enumeration keys = request.getAttributeNames();
			while (keys.hasMoreElements()) {
				String key = keys.nextElement().toString();
				logger.debug(key);
			}
			logger.debug(request.getAttribute("attr1").toString());
			logger.debug(this.getInitParameter("name"));
			logger.debug(this.getInitParameter("email"));
		}

		// response infos
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");

		String method = request.getParameter("method");
		logger.debug(method);

		PrintWriter out = null;
		try {
			out = response.getWriter();
			if (method == null || method.equals("")
					|| method.equals("queryUserList")) {
				JSONArray jsonArray = new JSONArray();				
				List list = userBpo.queryUsers(null);
				jsonArray.addAll(list);
				out.println(jsonArray.toString());
			} else if (method.equals("queryUser")) {
				String id = request.getParameter("userId");
				User user = null;
				if (BasicType.isNumber(id)) {
					int userId = Integer.valueOf(id).intValue();
					user = userBpo.queryUserByPk(userId);
				}
				out.println(JSONObject.fromObject(user).toString());
			} else if (method.equals("validateUser")) {
				String name = request.getParameter("userName");
				String pwd = request.getParameter("userPassword");
				int result = userBpo.validateUser(name, pwd);
				JSONObject jo = new JSONObject();
				jo.put("result", result);
				out.println(jo.toString());
			} else {
				JSONObject jo = new JSONObject();
				jo.put("result", "错误的方法");
				out.println(jo.toString());				
			}
				
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage());
			logger.error(e.getCause().getMessage());
			logger.error(e.getStackTrace().toString());
			e.printStackTrace();
		} finally {
			out.close();
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}

}
